<template>
  <div style="height: 100%;width: 100%">
    <el-row style="height: 100%;display: flex;width: 100%">
      <el-col :span="24" style="height: 100%;">
        <div style="height: 100%;">
          <el-row style="height: 150px;padding-top: 50px" type=“flex” align=“middle”>
            <SearchBox @search="search" @all="getPage" placeholder="请输入问卷名称"></SearchBox>
          </el-row>
<!--          <Question></Question>-->
          <el-row class="mb8" >
            <el-col :span="1.5">
              <router-link :to="{path:'/ViewQuestionnaireDetail',query:{id}}">
                <el-button type="primary" plain icon="el-icon-plus"
                           size="mini"
                >新增</el-button
                >
              </router-link>

            </el-col>
          </el-row>
          <el-row>
            <el-table
              :max-height="700"
              v-loading="loading" :data="questionnaireList"
              @selection-change="handleSelectionChange"
            >
              <el-table-column label="问卷编号" prop="id"
                               :show-overflow-tooltip="true" width="200"
              />
              <el-table-column label="问卷名称" prop="title"
                               :show-overflow-tooltip="true" width="300"
              />
              <el-table-column label="问卷创建时间" prop="createTime"
                               :show-overflow-tooltip="true" width="300"
              />
              <el-table-column label="填写人数" prop="num"
                               :show-overflow-tooltip="true" width="150"
              />
              <el-table-column label="状态" prop="publish"
                               :show-overflow-tooltip="true" width="150"
              />
              <el-table-column label="操作" align="center"
                               class-name="small-padding fixed-width"
              >
                <div slot-scope="scope" >
                  <el-button size="mini" type="text"
                             icon="el-icon-upload2" @click="handleAdd(scope.row)"
                             v-if="scope.row.publish === 0"
                  >发布</el-button
                  >
                  <el-button size="mini" type="text"
                             icon="el-icon-delete" @click="handleDelete(scope.row)"
                  >删除</el-button
                  >
                  <router-link :to="{path:'/ViewQuestionnaireDetail',query:{id:scope.row.id,name:scope.row.title}}">
                    <el-button size="mini" type="text"
                               icon="el-icon-edit"
                               v-if="scope.row.publish === 0"
                               @click="handleUpdate(scope.row)"
                    >编辑</el-button>
                  </router-link>

                </div>
              </el-table-column>
            </el-table>

            <pagination v-show="total > 0"
                        :total="total" :page.sync="queryParams.pageNum"
                        :limit.sync="queryParams.pageSize" @pagination="getPage"
            />

<!--            <el-dialog :title="diaTitle" :visible.sync="open" width="500px" append-to-body>-->
<!--              <el-form ref="form" :model="form" :rules="rules" label-width="100px">-->
<!--                <el-form-item label="问卷名称" prop="title">-->
<!--                  <el-input v-model="form.title" placeholder="请输入问卷名称" />-->
<!--                </el-form-item>-->
<!--              </el-form>-->
<!--              <div slot="footer" class="dialog-footer">-->
<!--                <el-button type="primary" @click="submitForm">确 定</el-button>-->
<!--                <el-button @click="cancel">取 消</el-button>-->
<!--              </div>-->
<!--            </el-dialog>-->
            <question :open="openAdd" :datas="questionList" @exit="cancel"></question>
<!--            <el-dialog :visible.sync="openAdd"-->
<!--                       @close="cancel"-->
<!--                       width="1000px" append-to-body>-->
<!--              <question :open="openAdd"></question>-->
<!--            </el-dialog>-->
          </el-row>
        </div>
      </el-col>
      <div style="clear:both"></div>
    </el-row>
  </div>
</template>
<script>
import Vertical from "@/layout/ManageMenu";
import Pagination from "@/components/Pagination";
import SearchBox from "@/views/reservation/search";
import {
  deleteQuestionnaire,
  pageQuestionnaire,
  publishQuestionnaire,
  queryQuestionnaireDetail, searchQuestionnaire
} from "@/api/questionnaire";
import Question from "@/views/consult/question";
import {deleteConsult} from "@/api/consult";
export default {
  name: 'ViewQuestionnaire',
  components: {
    Question,
    SearchBox,
    Vertical,
    Pagination
  },
  data() {
    return {
      id:0,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showValue: true,
      // 总条数
      total: 0,
      // 弹出层标题
      diaTitle: "",
      // 是否显示弹出层
      open: false,

      openAdd:false,

      scoreOpen: false,
      scores: {},

      searchValue:false,
      // 日期范围
      dateRange: [],
      // 菜单列表
      menuOptions: [],
      // 部门列表
      deptOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        roleName: undefined,
        roleKey: undefined,
        status: undefined,
      },
      // 表单参数
      form: {},
      questionList: {
        itemList: [],
      },
      // 表单校验
      rules: {
        startTime: [{ required: true, message: "开始时间不能为空", trigger: "blur" }],
        endTime: [{ required: true, message: "结束时间不能为空", trigger: "blur" }],
        location: [{ required: true, message: "地点不能为空", trigger: "blur" }],
      },
      defaultProps: {
        children: "children",
        label: "label",
      },

      statusList:[
        {
          id:0,
          label:"待进行"
        },
        {
          id:1,
          label:"已完成"
        },
      ],
    };
  },
  created() {
    // this.getList();
    this.getPage(this.queryParams);
  },
  methods: {
    search(data){
      this.searchValue=data;
      this.loading=true;
      searchQuestionnaire({title:data,pageNum:this.queryParams.pageNum,pageSize:this.queryParams.pageSize})
        .then((response) => {
          if (response.success) {
            this.questionnaireList = response.data.records;
            this.total = response.data.total;//重要，会决定有几个页码按钮
            this.loading = false;
          }
        })
        .catch((e) => {
          _this.$message.error(e, "出现未知错误");
        });
    },
    //分页查询
    getPage(data) {
      if(data==null){
        data=this.queryParams
      }
      this.openAdd=false;
      this.loading = true;
      // this.openAdd=false;
      pageQuestionnaire(data)
        .then((response) => {
          if (response.success) {
            this.questionnaireList = response.data.records;
            if(this.total===0){
              this.total = response.data.total;//重要，会决定有几个页码按钮
            }
            // this.openAdd=false;
            this.loading = false;
            // this.openAdd=false;
          }
        })
        .catch((e) => {
          _this.$message.error(e, "出现未知错误");
        });

    },
    // 新增按钮
    handleAdd(row){
      // this.reset();
      // // this.open=true;
      // this.open=true;
      // this.id=null;
      publishQuestionnaire(row.id)
    },

    handleDelete(row){
      // deleteQuestionnaire(row.id)
      //   .then((response) => {
      //     if (response.success) {
      //       alert("操作成功！")
      //     }
      //   })
      this.$modal
        .confirm('是否确认删除编号为"' + row.id + '"的问卷？')
        .then(function () {
          return deleteQuestionnaire(row.id);
        })
        .then((response) => {
          if (response.success) {
            this.$modal.msgSuccess("删除成功");
            this.getPage(this.queryParams);
          }
        })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openAdd=false;
      this.reset();
    },
    // 修改按钮
    handleUpdate(row) {
      this.reset();
      // queryQuestionnaireDetail()
      //   .then((response) => {
      //     if (response.success) {
      //       this.questionList.itemList = response.data.questionList;
      //       this.name=response.data.name;
      //       // if(this.total===0){
      //       //   this.total = response.data.total;//重要，会决定有几个页码按钮
      //       // }
      //       // this.loading = false;
      //     }
      //   })
      this.questionnaireList.forEach((item) => {
        if (item.id === row.id) {
          this.form = JSON.parse(JSON.stringify(item));
          this.open = true;
          // this.diaTitle = "修改问卷";
        }
      });
    },
    // 表单重置
    reset() {
      this.form = {
        startTime: undefined,
        endTime:undefined,
        location:undefined,
      };
    },
  },
}
</script>

<style lang="scss" scoped>
.auto-generate-form {
  padding-top: 50px;
  padding-right: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
</style>
